package fortunata.util;

import java.util.Date;

/**
 * Utility
 */
public class NiceDate {
    
    public static String niceDateIntervalTillNow(Date from){
        return niceDateInterval(from, new Date()) + " ago";
    }
    public static String niceDateInterval(Date from, Date to){
        long millisDif = to.getTime() - from.getTime();
        long secs = millisDif / 1000;
        long mins = secs / 60;
        long hours = mins / 60;
        long days = hours / 24;
        long weeks = days / 7;
        long aproxMonths = (long)(days / 30.5);
        long aproxYears = (long)(days / 365.24);

        if (secs < 1){
            return " < 1 sec";
        }
        if (secs >= 1 && mins < 1){
            return secs + ((secs == 1)? " sec":" secs");
        }
        if (mins >= 1 && hours < 1){
            return mins + ((mins == 1)? " min":" mins");
        }
        if (hours >= 1 && days < 1){
            return hours + ((hours == 1)? " hour":" hours");
        }
        if (days >= 1 && weeks < 1){
            return days + ((days == 1)? " day":" days");
        }
        if (weeks >= 1 && aproxMonths < 1){
            return weeks + ((weeks == 1)? " week":" weeks");
        }
        if (aproxMonths >= 1 && aproxYears < 1){
            return  "~"+ aproxMonths + ((aproxMonths == 1)? " month":" months");
        }
        return ">" + aproxYears + ((aproxYears == 1)? " year":" years");
    }
    public static void main(String[]args){
        Date now = new Date();
        long oneSec = 1000;
        long oneMin = 1000 * 60;
        long oneHour = oneMin * 60;
        long oneDay = oneHour * 24;
        long oneWeek = oneDay * 7;
        long oneMonthAprox = Math.round(oneDay * 30.5);
        long oneYearAprox = Math.round(oneDay * 365.24);

        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneSec - 3 * 100)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneMin - 3 * oneSec)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneHour - 3 * oneMin)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneDay - 3 * oneHour)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneWeek - 3 * oneDay)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 5 * oneWeek)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneMonthAprox - 3 * oneWeek)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 13 * oneMonthAprox)));
        System.out.println (niceDateIntervalTillNow(new Date (now.getTime() - 3 * oneYearAprox - 3 * oneMonthAprox)));
    }
}
